<html>

	<head>
		<title>闻达搜索</title>
		<meta charset="utf-8">
		<meta name="apple-mobile-web-app-capable" content="yes" />
		<meta name="mobile-web-app-capable" content="yes" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=650,  user-scalable=no">
		<link rel="shortcut icon" href="#" />

		<link href="/static/mdi_font/css/materialdesignicons.min.css" rel="stylesheet">
		<link href="/static/vuetify.min.css" rel="stylesheet">
		<script src="/static/vue.js"></script>
		<script src="/static/vuetify.min.js"></script>
		<script src="/static/markdown-it.min.js"></script>
		<style>
		div {

			transition: all 0.3s;
		}

		.v-sheet.v-card {
			margin: 20px;
			padding: 10px;
		}

		.logo-center {
			left: calc(50% - 140px);
			width: 260px;
			font-size: 4em;
			padding-top: 2.5em;
			padding-bottom: 0.8em;
			position: relative;
		}

		.logo-left {
			left: 20px;
			width: 100%;
			font-size: xx-large;
			position: relative;
		}

		.input-box {
			margin: auto;
			width: fit-content;
			min-width: 580px;
		}

		pre {
			margin: 20px;
			white-space: break-spaces;
		}

		.v-application--wrap {
			display: unset;
		}
	</style>
	</head>

	<body>
		<div id="app" v-cloak>
			<v-app>
				<v-combobox multiple small-chips dense hide-details="auto" v-model="s查询参数"
					outlined label="知识库" class="ma-2"
					:items="items"></v-combobox>
				<a style="float: right;" href="/static/st.html">资料管理</a>
				<div :class="(s关键词.length!=0||results.length!=0)?'logo-left':'logo-center'"><b
						style="color: purple;">闻达</b><b>搜索</b></div>

				<v-row class="input-box">
					<v-col cols="10">
						<v-textarea autofocus v-model="s关键词" label="关键词" no-resize rows="1"
							hide-details="auto"
							@keypress.enter="submit"></v-textarea>
					</v-col>
					<v-col cols="2">
						<v-btn color="purple" dark size="x-large" @click="submit()">
							搜索
						</v-btn>
					</v-col>
				</v-row>
				<v-card v-ripple elevation="2" v-if="s回答.length">
					<pre>{{s回答}}</pre>
				</v-card>
				<v-card v-ripple elevation="2" v-for="result in results"
					@click.stop='result.url&&window.open(result.url,"_blank")'>
					<v-card-title>{{result.title}}<v-chip class="ma-2"
							:color="result.score>i分数阈值?'green':'red'"
							text-color="white" v-text="result.score" v-if="result.score!=undefined"></v-chip></v-card-title>
					<v-divider></v-divider>
					<v-card-text v-text="result.content">
					</v-card>
					<v-snackbar v-model="b显示提示文本" :timeout="3000" style="white-space: pre-line">{{s提示文本}}</v-snackbar>
				</v-app>
			</div>
			<script>
		items = [
			'calc:50',
			'serper:50',
			'sogowx:50',
		]
		f读取现有库 = async () => {
			response = await fetch("/api/list_rtst_in_disk", {
				method: 'post',
			})
			let list_rtst_in_disk = await response.json()
			list_rtst_in_disk = list_rtst_in_disk.map(decodeURI).map(s => "rtst:10:" + s)
			app.items = list_rtst_in_disk.concat(app.items)
			let hash = decodeURI(document.location.hash.replace('#', ''))
			if (hash) {
				hash = hash.split(',')
			}
			else {
				hash = [app.items[0]]
			}
			app.s查询参数 = hash
		}
		app = new Vue({
			el: '#app',
			vuetify: new Vuetify(),
			data: () => ({
				s关键词: "",
				s回答: "",
				i分数阈值: 60,
				results: [],
				// 是否显示snackbar
				b显示提示文本: false,
				// snackbar的文本
				s提示文本: "",
				temperature: 0.8,
				top_p: 0.2,
				max_length: 2000,
				llm_type: "",
				s查询参数: '',
				items: items,
			}),
			methods: {
			},
			watch: {
				s查询参数: (s查询参数) => {
					document.location.hash = s查询参数.join(',')
				}
			}
		})
		f读取现有库()
		submit = async (e) => {
			e && e.preventDefault()
			window.ws && await ws.close();
			memory = await find_rtst_memory(app.s关键词, "rtst_kf", true)
			memory = memory.filter(i => !i.score || i.score < 200)
			if (memory.length > 0) {
				app.s回答 = memory[0].title
				app.results = memory.map(i => ({
					title: i.content,
					score: ((220 - i.score) / 220 * 100).toFixed(0),
					content: i.title
				}))
				return

			}
			kownladge = await find_dynamic(app.s关键词, 5, { libraryStategy: app.s查询参数.map(encodeURI).join(" "), maxItmes: 100 })
			kownladge = kownladge.map(i => ({
				title: get_title_form_md(i.title),
				score: ((220 - i.score) / 220 * 100).toFixed(0),
				url: get_url_form_md(i.title),
				content: i.content
			}))
			app.results = kownladge
			kownladge = kownladge.filter(i => !i.score || i.score > app.i分数阈值)
			if (kownladge.length > 0) {
				`if (app.llm_type == "rwkv") {
					let prompt = 'raw!Instruction: 深刻理解下面提供的信息，根据信息完成问答。\n\nInput: ' +
						kownladge.map((e, i) => i + 1 + "." + e.content).join('\n') + "\n\nResponse: Question: " + app.s关键词 + "\nAnswer: "
					return await send_raw(prompt, '', [], (s) => { app.s回答 = s })
				} else {
					let prompt = '总结下列与问题相关的信息。\n' +
						kownladge.map((e, i) => i + 1 + "." + e.content).join('\n') + "\n问题：" + app.s关键词
					return await send_raw(prompt, '', [], (s) => { app.s回答 = s })
				}`
			} else {
				alert("没有找到")
				//return await send(Q, keyword = Q, show = true, sources = sources)
			}
		}
		get_title_form_md = (s) => {
			try {
				return s.match('\\[(.+)\\]')[1]
			} catch {
				return s
			}
		}
		get_url_form_md = (s) => {
			try {
				return s.match('\\((.+)\\)')[1]
			} catch {
				return s
			}
		}
		alert = (text) => {
			app.s提示文本 = text; //.replace(/\n/g,"<br>")
			app.b显示提示文本 = true;
		}
	</script>
			<script src="wd_sdk.js"></script>
			<script src="api/llm"></script>
		</body>

	</html>